package com.example.boot.service.user;
import ch.qos.logback.core.pattern.ConverterUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.boot.bean.*;
import com.example.boot.config.RabbitMQConfig;
import com.example.boot.mapper.CountMapper;
import com.example.boot.mapper.PullMapper;
import com.example.boot.mapper.ReplyInfoMapper;
import com.example.boot.mapper.UserMapper;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;
@Service
public class UserServiceImpl implements UserService {


    @Autowired
    private ReplyInfoMapper replyInfoMapper;
    @Autowired
    private CountMapper countMapper;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private PullMapper pullMapper;


    //用户提交问题
    public void save(ReplyInfo replyInfo,int id) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        User user = userMapper.selectOne(queryWrapper);
        replyInfo.setUserName(user.getUserName());
        replyInfo.setUserId(user.getId());
        replyInfo.setStatus(0);
        Date date = new Date();
        replyInfo.setQuestionDate(date);
        replyInfoMapper.insert(replyInfo);

        //更新pull表
        QueryWrapper<Pull> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(Pull::getId,id);
        Pull pull = new Pull();
        pull.setUserId(user.getId());
        pull.setStatus(0);
        pullMapper.insert(pull);



        rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME,"boot.#",replyInfo);

    }

    @Autowired
    UserMapper userMapper;

    public User userLogin(User user) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_name",user.getUserName());
        wrapper.eq("password",user.getPassword());
        User user1 = userMapper.selectOne(wrapper);

        return user1;
    }



}
